/**   
 * @Title: TestLogServiceImplementation.java 
 * @Package cn.edu.cdtu.healthserver.service.impl 
 * @Description: TODO 
 * @author  
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthserver.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import cn.edu.cdtu.healthserver.dao.TestLogDao;
import cn.edu.cdtu.healthserver.dao.impl.TestLogDaoImplementation;
import cn.edu.cdtu.healthserver.domain.Test;
import cn.edu.cdtu.healthserver.domain.TestLog;
import cn.edu.cdtu.healthserver.domain.UserInformation;
import cn.edu.cdtu.healthserver.service.TestLogService;

/** 
 * @ClassName: TestLogServiceImplementation 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class TestLogServiceImplementation implements TestLogService {

	private TestLogDao testLogDao = new TestLogDaoImplementation();
	
	/* (non-Javadoc)
	 * <p>Title: getTestLogs</p> 
	 * <p>Description: </p> 
	 * @param user
	 * @return 
	 * @see cn.edu.scu.healthserver.service.TestLogService#getTestLogs(cn.edu.scu.healthserver.domain.User)
	 */
	@Override
	public List<TestLog> getTestLogs(UserInformation user) {
		// TODO Auto-generated method stub
		return testLogDao.query(user);
	}

	/* (non-Javadoc)
	 * <p>Title: getTestLogs</p> 
	 * <p>Description: </p> 
	 * @param user
	 * @param test
	 * @return 
	 * @see cn.edu.scu.healthserver.service.TestLogService#getTestLogs(cn.edu.scu.healthserver.domain.User, cn.edu.scu.healthserver.domain.Test)
	 */
	@Override
	public List<TestLog> getTestLogs(UserInformation user, Test test) {
		// TODO Auto-generated method stub
		return testLogDao.query(user, test);
	}

	/* (non-Javadoc)
	 * <p>Title: createTestLog</p> 
	 * <p>Description: </p> 
	 * @param user
	 * @param test
	 * @return 
	 * @see cn.edu.scu.healthserver.service.TestLogService#createTestLog(cn.edu.scu.healthserver.domain.User, cn.edu.scu.healthserver.domain.Test)
	 */
	@Override
	public TestLog createTestLog(UserInformation user, Test test) {
		// TODO Auto-generated method stub
		TestLog newLog = new TestLog(
				user.getId(),
				test.getId(),
				new Timestamp(((new Date().getTime()+500)/1000)*1000),
				0
				);
		if(!testLogDao.add(newLog)){
			return null;
		}
		List<TestLog> logs = this.getTestLogs(user, test);
		for (TestLog testLog : logs) {
			if(newLog.equalsWithoutId(testLog)){
				return testLog;
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * <p>Title: updateTestPoint</p> 
	 * <p>Description: </p> 
	 * @param testLog
	 * @param point
	 * @return 
	 * @see cn.edu.scu.healthserver.service.TestLogService#updateTestPoint(cn.edu.scu.healthserver.domain.TestLog, int)
	 */
	@Override
	public String updateTestPoint(TestLog testLog, int point) {
		// TODO Auto-generated method stub
		String resultString = null;
		testLog.setPoint_sum(point);
		if (testLogDao.update(testLog)) {
			resultString = "更新测试分数成功！";
		} else {
			resultString = "更新测试分数失败！";
		}
		return resultString;
	}

}
